#!/usr/bin/perl


################################################################
# Author   : DebugProject_Bourdin
# Created  : 27/02/2007
# Modified : 28/02/2007
# Script   : generate the 2 files corresponding to the logFiles 
# 	     and the command diff result. those files will
#	     be used for the comparison (to color the xml sheet)
################################################################


#function Max
sub Max{
	my($max)=shift(@_);
	foreach $i (@_){
		$max = $i if($max<$i);
	}
	return $max;
}


#check the command line
if(scalar(@ARGV) !=6){die "[way to use : <execFile1> <execFile2> <diffResult> <result1> <result2> <pathToScript>]"}

$exec_file1 = $ARGV[0];
$exec_file2 = $ARGV[1];
$diff_result = $ARGV[2];
$result1 = $ARGV[3];
$result2 = $ARGV[4];
$pathToScript = $ARGV[5];

#check that exec_file1 exists
if(!(-e $exec_file1)){die "[file <execFile1> doesn't exist]"}

#check that exec_file2 exists
if(!(-e $exec_file2)){die "[file <execFile2> doesn't exist]"}

#check that diff_result exists
if(!(-e $diff_result)){die "[file <diffResult> doesn't exist]"}

#check that result1 doesn't exist
if((-e $result1)){die "[file <res1> already exists]"}

#check that result2 doesn't exist
if((-e $result2)){die "[file <res2> already exists]"}


#Openinning handles
open(EXEC1,"<$exec_file1") || die "[Can't open $exec_file1]";
open(EXEC2,"<$exec_file2") || die "[Can't open $exec_file2]";
open(DIFF,"<$diff_result") || die "[Can't open $diff_result]";

open(RES1,">$result1");
open(RES2,">$result2");


#the cpt variable is used to know when we have processed on the line where the execID is written. In fact, we don't want to
#consider the execID as a difference !
$cpt = 0;

#initialistion of the current line of the two result files. These variables are used to reference the current lines of the
#output files. 
$indCurLi1 = 1;
$indCurLi2 = 1;

#print the xml version, the path to scripts... in the result files
print RES1 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n<?xml-stylesheet href=\"".$pathToScript."colorCmp.xsl\" type=\"text/xsl\"?>\n\n";

print RES2 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n<?xml-stylesheet href=\"".$pathToScript."colorCmp.xsl\" type=\"text/xsl\"?>\n\n";


#while diff_result is not empty
while($li_diff_file=<DIFF>){

	#process for type_a lines (type_a refers to diff result)
	#in this part, we will color the lines THAT HAVE BEEN ADDED in file 2, we will jump lines in file 1 
	#   ---> (color=\"A\-S\") added in result2
	if($li_diff_file=~/^(\d+)a(\d+)\,?(\d*).*$/){
		#line from where we have to jump in file 1
		$begin_line1 = $1;
		#first corresponding line in file 2
		$begin_line2 = $2;
		#last corresponding line in file 2
		$end_line2 = $3;
		#if end_line2 = 0 then we just have to jump one line (the jump size is represented by delta)
		if($end_line2 eq ""){
			$delta=0;
		}
		else{
			$delta = ($end_line2-$begin_line2);
		}
		
		#while the current line of file 1 isn't the line where we have to jump, we just copy the line		
		while($indCurLi1 <= $begin_line1){
			$curLi1=<EXEC1>;
			print RES1 $curLi1;
			$indCurLi1++;
		}
		
		#while the current line of file 2 isn't the first line we have to color , we just copy the line
		while($indCurLi2 <= $begin_line2-1){
			$curLi2=<EXEC2>;
			print RES2 $curLi2;
			$indCurLi2++;
		}
		
		$i=0;
		while($i <= $delta){
			#line jump in File1
			print RES1 "<msg> <newline/> </msg>\n";
			
			#coloration in File2
			$curLi2=<EXEC2>;
			$curLi2=~/^(\<msg\s)(.*)$/;
			$beginCurLi2 = $1;
			$endCurLi2 = $2;
			print RES2 $beginCurLi2."color=\"A\-S\" ".$endCurLi2."\n";
			$indCurLi2++;
			
			$i++;
		}		
	}
	
	#process for type_d lines (type_d refers to diff result)
	#in this part, we will color the lines THAT HAVE BEEN ADDED in file 1, we will jump lines in file 2
	#   ---> (color=\"A\-S\")  added in result1
	elsif($li_diff_file=~/^(\d+)\,?(\d*)d(\d+).*$/){
		#first corresponding line in file 1
		$begin_line1 = $1;
		#last corresponding line in file 1
		$end_line1 = $2;
		#line from where we have to jump in file 2
		$begin_line2 = $3;
		#if end_line1 = 0 then we just have to jump one line (the jump size is represented by delta)
		if($end_line1 eq ""){
			$delta=0;
		}
		else{
			$delta = ($end_line1-$begin_line1);
		}
		
		#while the current line of file 1 isn't the first line we have to color , we just copy the line
		while($indCurLi1 <= $begin_line1-1){
			$curLi1=<EXEC1>;
			print RES1 $curLi1;
			$indCurLi1++;
		}
		
		#while the current line of file 2 isn't the line where we have to jump, we just copy the line
		while($indCurLi2 <= $begin_line2){
			$curLi2=<EXEC2>;
			print RES2 $curLi2;
			$indCurLi2++;
		}
		
		$i=0;
		while($i <= $delta){
			#line jump in File2
			print RES2 "<msg> <newline/> </msg>\n";
			
			#coloration in File1
			$curLi1=<EXEC1>;
			$curLi1=~/^(\<msg\s)(.*)$/;
			$beginCurLi1 = $1;
			$endCurLi1 = $2;
			print RES1 $beginCurLi1."color=\"A\-S\" ".$endCurLi1."\n";
			$indCurLi1++;
			
			$i++;
		}
	}

	#process for type_c lines (type_c refers to diff result)
	#in this part, we will color the lines THAT HAVE CHANGED between file 1 and file 2
	#   ---> ("color=\"C\")  added in result1 and result2
	elsif($li_diff_file=~/^(\d+)\,?(\d*)c(\d+)\,?(\d*).*$/){
		#first corresponding line in file 1
		$begin_line1 = $1;
		#last corresponding line in file 1
		$end_line1 = $2;
		
		#first corresponding line in file 2
		$begin_line2 = $3;
		#last corresponding line in file 2
		$end_line2 = $4;
		
		#not to considerate execID as a difference
		if(($cpt==0)){
			#if the difference comes from the execID, we just copy the lines, for file 1 and file 2
			while($indCurLi1 <= ($begin_line1+1) ){
				$curLi1=<EXEC1>;
				print RES1 $curLi1;
				$indCurLi1++;
			}
		
			while($indCurLi2 <= ($begin_line2+1) ){
				$curLi2=<EXEC2>;
				print RES2 $curLi2;
				$indCurLi2++;
			}
		}
		
		else{
			#while the current line of file 1 isn't the first line we have to color , we just copy the line
			while($indCurLi1 <= $begin_line1-1){
				$curLi1=<EXEC1>;
				print RES1 $curLi1;
				$indCurLi1++;
			}
		
			#while the current line of file 2 isn't the first line we have to color , we just copy the line
			while($indCurLi2 <= $begin_line2-1){
				$curLi2=<EXEC2>;
				print RES2 $curLi2;
				$indCurLi2++;
			}
			
			#if end_line1 = 0 then we just have to color one line for file1
			if($end_line1 eq ""){
				$delta1=0;
			}
			else{
				#else the number line we have to color is represented by delta1
				$delta1 = ($end_line1-$begin_line1);
			}

			#if end_line2 = 0 then we just have to have to color one line for file2
			if($end_line2 eq ""){
				$delta2=0;
			}		
			else{
				#else the number line we have to color is represented by delta2
				$delta2 = ($end_line2-$begin_line2);
			}
			
			#to get the maximum between delta1 and delta2
			$maxi = &Max($delta1,$delta2);
			
			$i1=0;
			$i2=0;
			
			#if the maximum is delta1, that means that we have to color (delta1 + 1) lines in file1
			#and that we have to color (delta2 + 1) lines in file 2 and finnaly that we have to jump
			#(delta1 - delta2) lines in file2
			if($maxi == $delta1){
				while($i1 <= $maxi){
					#coloration in File1
					$curLi1=<EXEC1>;
					$curLi1=~/^(\<msg\s)(.*)$/;
					$beginCurLi1 = $1;
					$endCurLi1 = $2;
					print RES1 $beginCurLi1."color=\"C\" ".$endCurLi1."\n";
					$indCurLi1++;
					
					$i1++;
				}
				
				while($i2 <= $delta2){
					#coloration in File2
					$curLi2=<EXEC2>;
					$curLi2=~/^(\<msg\s)(.*)$/;
					$beginCurLi2 = $1;
					$endCurLi2 = $2;
					print RES2 $beginCurLi2."color=\"C\" ".$endCurLi2."\n";
					$indCurLi2++;
					
					$i2++;
				}
				while($i2 <= $maxi){
					#line jump in File2
					print RES2 "<msg color=\"C\"> <newline/> </msg>\n";
					
					$i2++;
				}
				
			}
			#else the maximum is delta2 : that means that we have to color (delta2 + 1) lines in file2
			#and that we have to color (delta1 + 1) lines in file 1 and finnaly that we have to jump
			#(delta2 - delta1) lines in file1
			else{
				while($i2 <= $maxi){
					#coloration in File2
					$curLi2=<EXEC2>;
					$curLi2=~/^(\<msg\s)(.*)$/;
					$beginCurLi2 = $1;
					$endCurLi2 = $2;
					print RES2 $beginCurLi2."color=\"C\" ".$endCurLi2."\n";
					$indCurLi2++;
					
					$i2++;
				}
				
				while($i1 <= $delta1){
					#coloration in File1
					$curLi1=<EXEC1>;
					$curLi1=~/^(\<msg\s)(.*)$/;
					$beginCurLi1 = $1;
					$endCurLi1 = $2;
					print RES1 $beginCurLi1." color=\"C\" ".$endCurLi1."\n";
					$indCurLi1++;
					
					$i1++;
				}
				while($i1 <= $maxi){
					#line jump in File1
					print RES1 "<msg color=\"C\"> <newline/> </msg>\n";
					
					$i1++;
				}
			}
			
		}
	
	}
	
	#process for normal lines
	else{
		#nothing to do, in fact we don't color those lines and there are copied before in this script 
	}		
	
	#to indicate that we have already processed on the execID difference
	$cpt++;
}

#at this point, the diffFile is empty, so we just have to copy the lines... for file 1 and file 2
while($curLi1=<EXEC1>){
	print RES1 $curLi1;
}
	
while($curLi2=<EXEC2>){
	print RES2 $curLi2;
}

		

#Closing handles 		
close EXEC1;
close EXEC2;
close DIFF;
close RES1;
close RES2;
